import 'package:common/common.dart';
import 'package:flutter/foundation.dart';

/// 本地语言切换支持
const List<Locale> supportLocale = [
  Locale.fromSubtags(languageCode: "en", countryCode: "US"),
  Locale.fromSubtags(languageCode: "zh", scriptCode: "Hans", countryCode: "CN"),
];

/// 语言本地处理
class AppLocalizations {
  final Locale locale;

  AppLocalizations(this.locale);

  static final Map<String, Map<String, String>> _localizedValues = {
    "zh": {
      "appName": "手机应用",
    },
    "en": {
      "appName": "Farm Watcher",
    },
  };

  String get appName {
    return _localizedValues[locale.languageCode]?["appName"] ?? "Farm Watcher";
  }

  static AppLocalizations of(BuildContext context) {
    return Localizations.of(context, AppLocalizations);
  }
}

/// 本地语言代理器
class AppLocalizationsDelegate extends LocalizationsDelegate<AppLocalizations> {
  static AppLocalizationsDelegate delegate = AppLocalizationsDelegate();

  @override
  bool isSupported(Locale locale) {
    return supportLocale.any((element) => element.languageCode == locale.languageCode);
  }

  @override
  Future<AppLocalizations> load(Locale locale) {
    return SynchronousFuture(AppLocalizations(locale));
  }

  @override
  bool shouldReload(covariant LocalizationsDelegate<AppLocalizations> old) {
    return false;
  }
}
